package com.example.gateway.message;

import com.example.gateway.cluster.ClusterManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import static com.example.common.constant.SystemConstant.MessageChannelNamePrefix;

@Component
public class MessageSender implements  MessageApi {

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    public ClusterManager clusterManager;

    /**
     * 发布消息
     * @param msg
     */
    public void  broadcast(String msg){

        clusterManager.getHashLoopMap().forEach((k, v)->{

            redisTemplate.convertAndSend(MessageChannelNamePrefix + k,v);

        });

    }

}
